* Table A30: Re-weighted Intergenerational Mobility Estimates

clear
use "${data_dir}DavisMazumderData.dta"

keep if age_firstSurvey<=18
drop if miss0 | miss1
drop if (mom_outOfRange==1 & linkMom==1 & !(dad_outOfRange==0 & linkDad==1)) | (dad_outOfRange==1 & linkDad==1 & !(mom_outOfRange==0 & linkMom==1)) | (linkMom==0 & linkDad==0) 
 


gen id = .
 replace id = id_son if survey==1966 & women==0
 replace id = id_daughter if survey==1966 & women==1
 replace id = CASEID if survey==1979
 
merge 1:1 id survey women using  ${data_dir}weight_adjustments.dta // Created by figureA11_A14.do




* ----------- Parent Weight Adjustments ---------------- *

replace wt_nls_parent = round(1000*wt_nls_parent)

* Generate income ranks after applying sample restrictions
gen N = .
 tab n  if surv79==0 & women==0  [w=wt_nls_parent]
 replace N = r(N) if surv79==0 & women==0 
 tab n  if surv79==0 & women==1   [w=wt_nls_parent]
 replace N = r(N) if surv79==0 & women==1 
 tab n  if surv79==1 & women==0   [w=wt_nls_parent]
 replace N = r(N) if surv79==1 & women==0 
 tab n  if surv79==1 & women==1 [w=wt_nls_parent]
 replace N = r(N) if surv79==1 & women==1 
 
* Family Income Rank in Parent Generation
set sortseed 6688571
sort women surv79 faminc0
by women surv79: gen __rank0 = sum(wt_nls_parent) 
gen _rank0 = __rank0/N 
by women surv79 faminc0: egen rank0 = mean(_rank0) 
 replace rank0 = 100*rank0
gen perc66 = rank0*(1-surv79)
gen perc79 = rank0*surv79

 
* Family Income Rank in Kid Generation
set sortseed 6688571
sort women surv79 faminc1
by women surv79: gen __rank1 = sum(wt_nls_parent)
gen _rank1 = __rank1/N 
bys women surv79 faminc1: egen rank1 = mean(_rank1) 
replace rank1 = 100*rank1

drop N __rank0 _rank0 __rank1 _rank1 


* --------- Rank-Rank --------- *

* Pooled
reg rank1 perc66 perc79 surv79 women [w=wt_nls_parent] , cluster(hhid)
eststo rank_parent
test perc79=perc66
estadd scalar p = r(p): rank_parent
gen s=e(sample)
count if surv79==0 & s==1
estadd scalar N66 = r(N): rank_parent
count if surv79==1 & s==1
estadd scalar N79 = r(N): rank_parent
drop s
lincom perc79-perc66
estadd scalar diff = r(estimate): rank_parent
estadd scalar se = r(se): rank_parent





* --------- IGE --------- *

* Pooled
reg lfaminc1 loginc66 loginc79 surv79 women [w=wt_nls_parent], cluster(hhid)
eststo IGE_parent
test loginc79=loginc66
estadd scalar p = r(p): IGE_parent
gen s=e(sample)
count if surv79==0 & s==1
estadd scalar N66 = r(N): IGE_parent
count if surv79==1 & s==1
estadd scalar N79 = r(N): IGE_parent
drop s
lincom loginc79-loginc66
estadd scalar diff = r(estimate): IGE_parent
estadd scalar se = r(se): IGE_parent




* ----------- Kid Weight Adjustments ---------------- *

drop rank0 rank1 perc66 perc79

replace wt_nls_kid  = round(1000*wt_nls_kid)

* Generate income ranks after applying sample restrictions
gen N = .
 tab n  if surv79==0 & women==0  [w=wt_nls_kid]
 replace N = r(N) if surv79==0 & women==0 
 tab n  if surv79==0 & women==1   [w=wt_nls_kid]
 replace N = r(N) if surv79==0 & women==1 
 tab n  if surv79==1 & women==0   [w=wt_nls_kid]
 replace N = r(N) if surv79==1 & women==0 
 tab n  if surv79==1 & women==1 [w=wt_nls_kid]
 replace N = r(N) if surv79==1 & women==1 
 
* Family Income Rank in Parent Generation
set sortseed 6688571
sort women surv79 faminc0
by women surv79: gen __rank0 = sum(wt_nls_kid) 
gen _rank0 = __rank0/N 
by women surv79 faminc0: egen rank0 = mean(_rank0) 
 replace rank0 = 100*rank0
gen perc66 = rank0*(1-surv79)
gen perc79 = rank0*surv79

 
* Family Income Rank in Kid Generation
set sortseed 6688571
sort women surv79 faminc1
by women surv79: gen __rank1 = sum(wt_nls_kid)
gen _rank1 = __rank1/N 
bys women surv79 faminc1: egen rank1 = mean(_rank1) 
replace rank1 = 100*rank1

drop N __rank0 _rank0 __rank1 _rank1 


* --------- Rank-Rank --------- *

* Pooled
reg rank1 perc66 perc79 surv79 women [w=wt_nls_kid] , cluster(hhid)
eststo rank_kid
test perc79=perc66
estadd scalar p = r(p): rank_kid
gen s=e(sample)
count if surv79==0 & s==1
estadd scalar N66 = r(N): rank_kid
count if surv79==1 & s==1
estadd scalar N79 = r(N): rank_kid
drop s
lincom perc79-perc66
estadd scalar diff = r(estimate): rank_kid
estadd scalar se = r(se): rank_kid




* --------- IGE --------- *

* Pooled
reg lfaminc1 loginc66 loginc79 surv79 women [w=wt_nls_kid], cluster(hhid)
eststo IGE_kid
test loginc79=loginc66
estadd scalar p = r(p): IGE_kid
gen s=e(sample)
count if surv79==0 & s==1
estadd scalar N66 = r(N): IGE_kid
count if surv79==1 & s==1
estadd scalar N79 = r(N): IGE_kid
drop s
lincom loginc79-loginc66
estadd scalar diff = r(estimate): IGE_kid
estadd scalar se = r(se): IGE_kid









* ----------- Average Weight Adjustments ---------------- *

drop rank0 rank1 perc66 perc79


replace wt_nls_avg  = round(1000*wt_nls_avg)


* Generate income ranks after applying sample restrictions
gen N = .
 tab n  if surv79==0 & women==0  [w=wt_nls_avg]
 replace N = r(N) if surv79==0 & women==0 
 tab n  if surv79==0 & women==1   [w=wt_nls_avg]
 replace N = r(N) if surv79==0 & women==1 
 tab n  if surv79==1 & women==0   [w=wt_nls_avg]
 replace N = r(N) if surv79==1 & women==0 
 tab n  if surv79==1 & women==1 [w=wt_nls_avg]
 replace N = r(N) if surv79==1 & women==1 
 
* Family Income Rank in Parent Generation
set sortseed 6688571
sort women surv79 faminc0
by women surv79: gen __rank0 = sum(wt_nls_avg) 
gen _rank0 = __rank0/N 
by women surv79 faminc0: egen rank0 = mean(_rank0) 
 replace rank0 = 100*rank0
gen perc66 = rank0*(1-surv79)
gen perc79 = rank0*surv79

 
* Family Income Rank in Kid Generation
set sortseed 6688571
sort women surv79 faminc1
by women surv79: gen __rank1 = sum(wt_nls_avg)
gen _rank1 = __rank1/N 
bys women surv79 faminc1: egen rank1 = mean(_rank1) 
replace rank1 = 100*rank1

drop N __rank0 _rank0 __rank1 _rank1 


* --------- Rank-Rank --------- *

* Pooled
reg rank1 perc66 perc79 surv79 women [w=wt_nls_avg] , cluster(hhid)
eststo rank_avg
test perc79=perc66
estadd scalar p = r(p): rank_avg
gen s=e(sample)
count if surv79==0 & s==1
estadd scalar N66 = r(N): rank_avg
count if surv79==1 & s==1
estadd scalar N79 = r(N): rank_avg
drop s
lincom perc79-perc66
estadd scalar diff = r(estimate): rank_avg
estadd scalar se = r(se): rank_avg





* --------- IGE --------- *

* Pooled
reg lfaminc1 loginc66 loginc79 surv79 women [w=wt_nls_avg], cluster(hhid)
eststo IGE_avg
test loginc79=loginc66
estadd scalar p = r(p): IGE_avg
gen s=e(sample)
count if surv79==0 & s==1
estadd scalar N66 = r(N): IGE_avg
count if surv79==1 & s==1
estadd scalar N79 = r(N): IGE_avg
drop s
lincom loginc79-loginc66
estadd scalar diff = r(estimate): IGE_avg
estadd scalar se = r(se): IGE_avg


#delimit ;
estout rank_parent IGE_parent rank_kid IGE_kid rank_avg IGE_avg
	using  "${results_dir}tableA30.txt", replace
	keep(perc66 perc79) 
	rename(loginc66  perc66 loginc79 perc79)
	cells(b(star fmt(3))  se(par fmt(3))) 
	collabels(,none) stat(diff se p N66 N79) stardrop(*) ;	
#delimit cr	

